<?php
class MenusController extends MenuAppController {
	public $uses = array ('Menu.Menu');

	/**
	 * 菜单列表
	 * @author lj@gootop.net  
	 * @Date 2012-11-30
	 * @param
	 * @return 
	 */
	function admin_index(){
		$url = array('admin'=>true,'plugin'=>'menu','controller'=>'menus','action'=>'ajaxJsonPage');
		$param = $this->Menu->pageParam($url);
		$this->set('paginationScript',$this->Menu->showTreePagination($param));
	}
	/**
	 *
	 * 用户查询数据
	 * @author lj@gootop.net
	 * @Date 2012-11-29
	 * @param
	 * @return
	 */
	function admin_ajaxJsonPage(){
		$this->autoRender = false;
	
		$return = array(
				'page'=>0,
				'total'=>0,
				'records'=>0,
				'rows'=>array()
		);
		$parentId = empty($_POST['nodeid'])?1:$_POST['nodeid'];
		$level = empty($_POST['n_level'])?0:$_POST['n_level'];
		$rows = $this->Menu->ajaxTreeCategory($parentId,$level+1);
		$return['rows'] =$rows;
	
		echo json_encode($return);
	}
	/**
	 * 添加
	 * @author lj@gootop.net  
	 * @Date 2012-11-30
	 * @param
	 * @return 
	 */
	function admin_add($parent_id=1) {
		if ($this->request->is('post')) {
			$this->Menu->create();
			if ($this->Menu->save($this->data)) {
				$this->Session->setFlash(__('操作成功！'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('操作失败！'));
			}
		}
		$this->set('parent_id',$parent_id);
	}
	/**
	 * 修改
	 * @author lj@gootop.net
	 * @Date 2012-11-29
	 * @param
	 * @return
	 */
	function admin_edit($id = null) {
		$this->Menu->id = $id;
		if (!$this->Menu->exists()) {
			throw new NotFoundException(__('Invalid sys area'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Menu->save($this->request->data)) {
				$this->Session->setFlash(__('操作成功！'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('操作失败！'));
			}
		} else {
			$this->request->data = $this->Menu->read(null, $id);
		}
	}

	/**
	 * 取得权限菜单
	 * @author zmx@gootop.net  
	 * @Date 2012-12-3
	 * @param
	 * @return 
	 */
	function prowerMenu($deep = 1){
		$menu = $this->Menu->getTreeProwerMenu($deep);
		$this->set('treeMenu',$menu);
	}
	
}
